﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.CustomerManage;
using DevExpress.XtraGrid.Views.Grid;
using System.Runtime.InteropServices;
using MINERP.ComControls;
using MINERP.ComControls.Extension;
using MINERP.COM;


namespace MINERP.CustomerManage.OrderInfo
{
    public partial class FrmOrderDelyInfo : FrmParent
    {
        //参数
        private OrderInfoBLL bll = new OrderInfoBLL();
        private int PageCount = 0;                                                  //每页的产品数量
        private int CurrentPage = 1;                                                //当前页数
        //private int TotalPage = 0;                                                  //页总数
        private int OrderInfoNum = 0;                                               //产品总数
        private int CurrentOrderInfoStatus = 0;                                     //当前筛选的产品状态
        private List<OrderInfoBLL.OrderProductUser> OrderProducts = new List<OrderInfoBLL.OrderProductUser>();      //产品控件的数据源 
        private bool IsCanPriority = false;                                         //是否能执行加急操作
        [DllImport("shell32.dll ")]
        private static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);


        //属性
        private static FrmOrderDelyInfo _instance;
        public static FrmOrderDelyInfo GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmOrderDelyInfo();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }
        public FrmManage Manage;                                                //客服主界面

        //修改的加急级别
        public int Priority
        {
            get;
            set;
        }

        public FrmOrderDelyInfo()
        {
            InitializeComponent();
            base.InitiAuthority();

            gvOrderInfo.ShowRowIndicator(50);
        }

        private void FrmOrderInfo_Load(object sender, EventArgs e)
        {
            try
            {
                //时间范围
                dtStart.DateTime = DateTime.Now.Date;
                dtEnd.DateTime = DateTime.Now.AddDays(1).Date;
                dtFindStart.DateTime = DateTime.Now.AddDays(-5).Date;
                dtFindEnd.DateTime = DateTime.Now.Date;
                //产品状态
                GetOrderInfoStatus();
                //cmbPageCount_SelectedIndexChanged(sender, e);
                BindDataByTime();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //设置每页产品数量
        private void cmbPageCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int count = Convert.ToInt32(cmbPageCount.Text);
                PageCount = count;
                SetBase();
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //产品的状态
        private void tsOrderInfoStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //批量加急
                if (tsOrderInfoStatus.Text == "正在生产")
                {
                    tsPriority.Enabled = true;
                }
                else
                {
                    tsPriority.Enabled = false;
                }

                if (tsOrderInfoStatus.Text.Contains("审核") || tsOrderInfoStatus.Text.Contains("不足"))
                {
                    if (tsOrderInfoStatus.Text.Equals("远程开单等待审核"))
                        toolStripButtonAutoOederCheck.Enabled = true;
                    else
                    {
                        btnCheckOrder.Enabled = true;
                    }
                }
                else
                {
                    btnCheckOrder.Enabled = false;
                    toolStripButtonAutoOederCheck.Enabled = false;
                }
                GetOrderInfoStatus();
                //SetBase();
                //BindData();
                BindDataByTime();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //设置行背景色
        private void gvOrderInfo_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            try
            {
                if (e.RowHandle % 2 == 1)
                {
                    e.Appearance.BackColor = Color.LightGray;
                    //e.Appearance.BackColor2 = Color.LightGray;
                }


                if (e.RowHandle > -1)
                {
                    MINERP.BLL.CustomerManage.OrderInfoBLL.OrderProductUser oi = (OrderInfoBLL.OrderProductUser)gvOrderInfo.GetRow(e.RowHandle);
                    if (oi.FetchTime <= DateTime.Now && oi.Status != (int)OrderInfoStatus.Completed)
                    {
                        e.Appearance.BackColor = Color.Red;
                    }
                    if (oi.Status == (int)OrderInfoStatus.Deleted)
                    {
                        e.Appearance.BackColor = Color.Gray;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //上一页
        private void linkPre_Click(object sender, EventArgs e)
        {
            try
            {
                if (OrderInfoNum == 0)
                    return;
                try
                {
                    CurrentPage = Convert.ToInt32(txtPageNumber.Text);      //当前页
                }
                catch
                {
                    MessageBox.Show("页码的格式不正确！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtPageNumber.SelectAll();
                    txtPageNumber.Focus();
                    return;
                }
                if (CurrentPage == 1)
                {
                    MessageBox.Show("已经是第一页！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                CurrentPage--;
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        //下一页
        private void linkNext_Click(object sender, EventArgs e)
        {
            try
            {
                int totalPage = Convert.ToInt32(txtTotalPageCount.Text);
                try
                {
                    CurrentPage = Convert.ToInt32(txtPageNumber.Text);
                }
                catch
                {
                    MessageBox.Show("页码的格式不正确！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtPageNumber.SelectAll();
                    txtPageNumber.Focus();
                    return;
                }
                if (CurrentPage >= totalPage)
                {
                    MessageBox.Show("已经是最后一页！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                CurrentPage++;
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //刷新
        private void tsRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                //BindData();
                BindDataByTime();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //查询
        private void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                /*
                Cursor = Cursors.WaitCursor;
                OrderInfoBLL.OrderInfoCondition orderInfoCondition = new OrderInfoBLL.OrderInfoCondition();
                orderInfoCondition.OrderProductCode = txtOrderProductCode.Text.Trim();
                orderInfoCondition.CtmName = txtCtmName.Text.Trim();

                OrderInfoBLL.OrderInfoNoCondition orderInfoNoCondition = new OrderInfoBLL.OrderInfoNoCondition();
                orderInfoNoCondition.PhotoCode = txtPhotoCode.Text.Trim();

                OrderProducts = bll.FindOrderInfo(orderInfoCondition, orderInfoNoCondition);
                gcOrderInfo.DataSource = OrderProducts;
                gcOrderInfo.RefreshDataSource();
                if (OrderProducts == null)
                    lblCount.Text = "0";
                else
                    lblCount.Text = OrderProducts.Count.ToString();
                Cursor = Cursors.Default;*/
                Cursor = Cursors.WaitCursor;
                string productCode = txtOrderProductCode.Text.Trim();
                string ctmName = txtCtmName.Text.Trim();
                string photoCode = txtPhotoCode.Text.Trim();
                string shopName = txtShopName.Text.Trim();
                DateTime start = dtFindStart.DateTime;
                DateTime end = dtFindEnd.DateTime.AddDays(1);
                if (productCode.Length == 0 && ctmName.Length == 0 && photoCode.Length == 0 && shopName.Length == 0)
                {
                    Cursor = Cursors.Default;
                    return;
                }
                if (photoCode.Length > 0)               //按照片编号查询
                {
                    OrderProducts = bll.FindOrderInfoByPhotoCodeProcedure(photoCode, start, end);
                }
                else
                {
                    //将中括号替换掉
                    ctmName = ctmName.Replace('[', '(');
                    ctmName = ctmName.Replace(']', ')');
                    OrderProducts = bll.FindOrderInfoProcedure(productCode, ctmName, shopName, start, end);
                }
                gcOrderInfo.DataSource = OrderProducts;
                gcOrderInfo.RefreshDataSource();
                if (OrderProducts == null)
                    lblCount.Text = "0";
                else
                    lblCount.Text = OrderProducts.Count.ToString();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //转到当前页
        private void btnPage_Click(object sender, EventArgs e)
        {
            try
            {
                int cpage = 1;
                try
                {
                    cpage = Convert.ToInt32(txtPageNumber.Text);
                }
                catch
                {
                    MessageBox.Show("页码的格式不正确！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtPageNumber.SelectAll();
                    txtPageNumber.Focus();
                    return;
                }
                int totalPage = Convert.ToInt32(txtTotalPageCount.Text);
                if (cpage > totalPage)
                    cpage = totalPage;
                CurrentPage = cpage;
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //"回车"转到当前页
        private void txtPageNumber_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.Enter)
                {
                    int cpage = 1;
                    try
                    {
                        cpage = Convert.ToInt32(txtPageNumber.Text);
                    }
                    catch
                    {
                        MessageBox.Show("页码的格式不正确！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtPageNumber.SelectAll();
                        txtPageNumber.Focus();
                        return;
                    }
                    int totalPage = Convert.ToInt32(txtTotalPageCount.Text);
                    if (cpage > totalPage)
                        cpage = totalPage;
                    CurrentPage = cpage;
                    BindData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //批量加急


        private void tsPriority_EnabledChanged(object sender, EventArgs e)
        {
            try
            {
                if (!tsPriority.Enabled)
                {
                    lblPriority.Visible = false;
                    cbPriority.Visible = false;
                    btnPriority.Visible = false;
                    btnPriorityFind.Visible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //单击批量加急
        private void tsPriority_Click(object sender, EventArgs e)
        {
            try
            {
                SetVisible();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //加急查询
        private void btnPriorityFind_Click(object sender, EventArgs e)
        {
            try
            {
                BindDataPriority();
                if (OrderProducts != null && OrderProducts.Count > 0)
                {
                    btnPriority.Enabled = true;
                    gvOrderInfo.SelectAll();
                }
                else
                {
                    btnPriority.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //加急按钮
        private void btnPriority_Click(object sender, EventArgs e)
        {
            try
            {
                int[] indexes = gvOrderInfo.GetSelectedRows();
                if (indexes.Length == 0)
                {
                    MessageBox.Show("没有产品被选中！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                FrmPriority p = FrmPriority.GetInstance;
                p.Delyinfo = this;
                if (DialogResult.OK != p.ShowDialog())
                {
                    return;
                }
                //批量加急
                List<int> orderProductIds = new List<int>();
                foreach (int index in indexes)
                {
                    OrderInfoBLL.OrderProductUser op = gvOrderInfo.GetRow(index) as OrderInfoBLL.OrderProductUser;
                    orderProductIds.Add(op.OrderProductId);
                }
                bll.SetPriority(orderProductIds, Priority);
                MessageBox.Show("批量加急成功！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                BindDataPriority();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //行索引
        private void gvOrderInfo_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        //---------------------------------------公共方法-----------------------------------------
        #region 设置基础信息
        /// <summary>
        /// 设置基础信息
        /// </summary>
        private void SetBase()
        {
            try
            {
                OrderInfoNum = bll.GetOrderInfoNum(CurrentOrderInfoStatus);
                int TotalPage = OrderInfoNum % PageCount == 0 ? (OrderInfoNum / PageCount) : (OrderInfoNum / PageCount + 1);
                txtTotalPageCount.Text = TotalPage.ToString();
                if (txtTotalPageCount.Text == "0")
                {
                    txtTotalPageCount.Text = "1";
                    TotalPage = 1;
                }
                //产品数量
                lblCount.Text = OrderInfoNum.ToString();
                try
                {
                    CurrentPage = Convert.ToInt32(txtPageNumber.Text);      //当前页
                }
                catch
                {
                    MessageBox.Show("页码请输入数字！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtPageNumber.SelectAll();
                    return;
                }
                if (CurrentPage > TotalPage)
                {
                    CurrentPage = TotalPage;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 设置数据源
        /// <summary>
        /// 设置数据源
        /// </summary>
        private void BindData()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                txtPageNumber.Text = CurrentPage.ToString();
                OrderProducts = GetCurrentPageData();
                gcOrderInfo.DataSource = OrderProducts;
                gcOrderInfo.RefreshDataSource();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                throw ex;
            }
        }
        #endregion

        #region 按时间设置数据源
        /// <summary>
        /// 按时间设置数据源
        /// </summary>
        private void BindDataByTime()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                OrderProducts = GetProductByTime();
                gcOrderInfo.DataSource = OrderProducts;
                gcOrderInfo.RefreshDataSource();
                if (OrderProducts == null)
                    lblCount.Text = "0";
                else
                {
                    lblCount.Text = OrderProducts.Count.ToString();
                }
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                throw ex;
            }
        }
        #endregion

        #region 设置批量加急数据源
        /// <summary>
        /// 设置批量加急数据源
        /// </summary>
        private void BindDataPriority()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                OrderProducts = GetProductPriority();
                gcOrderInfo.DataSource = OrderProducts;
                gcOrderInfo.RefreshDataSource();
                if (OrderProducts == null)
                    lblCount.Text = "0";
                else
                {
                    lblCount.Text = OrderProducts.Count.ToString();
                }
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                throw ex;
            }
        }
        #endregion

        #region 获取指定范围的产品
        /// <summary>
        /// 获取指定范围的产品
        /// </summary>
        /// <returns></returns>
        private List<OrderInfoBLL.OrderProductUser> GetCurrentPageData()
        {
            try
            {
                if (OrderInfoNum == 0)
                    return null;
                int currentPage = Convert.ToInt32(txtPageNumber.Text);      //当前页
                int start = (currentPage - 1) * PageCount + 1;
                int end = currentPage * PageCount;
                if (end > OrderInfoNum)
                    end = OrderInfoNum;
                return bll.GetOrderInfo(start, end, CurrentOrderInfoStatus);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 按时间范围获取产品
        /// <summary>
        /// 按时间范围获取产品
        /// </summary>
        /// <returns></returns>
        private List<OrderInfoBLL.OrderProductUser> GetProductByTime()
        {
            try
            {
                DateTime start = dtStart.DateTime;
                DateTime end = dtEnd.DateTime;
                if (end <= start)
                    return null;
                return bll.GetOrderInfoByTimeStoreProcedure(start, end, CurrentOrderInfoStatus);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 批量加急的产品列表
        /// <summary>
        /// 批量加急的产品列表
        /// </summary>
        /// <returns></returns>
        private List<OrderInfoBLL.OrderProductUser> GetProductPriority()
        {
            try
            {
                DateTime start = dtStart.DateTime;
                DateTime end = dtEnd.DateTime;
                if (end <= start)
                    return null;
                int priority = Convert.ToInt16(cbPriority.Text.Trim());
                return bll.GetOrderInfoByTimeStoreProcedure2(start, end, CurrentOrderInfoStatus, priority);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取产品状态
        /// <summary>
        /// 获取产品状态
        /// </summary>
        private void GetOrderInfoStatus()
        {
            try
            {
                toolStripButton1.Visible = false;
                string statusStr = tsOrderInfoStatus.Text;
                int status = 0;
                switch (statusStr)
                {
                    case "正在生产":
                        
                        status = (int)COM.OrderInfoStatus.Doing; break;
                    case "完成":
                        status = (int)COM.OrderInfoStatus.Completed; break;
                    case "全部":
                        status = -1;                            //-1表示全部产品
                        break;
                    case "停止生产":
                        status = (int)COM.OrderInfoStatus.Stop; break;
                    case "等待审核":
                        status = (int)COM.OrderInfoStatus.WaitCheck; break;
                    case "余额不足":
                        status = (int)COM.OrderInfoStatus.NoCheckCount; break;
                    case "移动照片":
                        toolStripButton1.Visible = true;
                        status = (int)COM.OrderInfoStatus.WaitPhotoMove; break;
                    case "已删除":
                        status = (int)COM.OrderInfoStatus.Deleted; break;
                    case "财务审核": status = (int)COM.OrderInfoStatus.WaitPay; break;
                    case "远程开单等待审核": status = (int)COM.OrderInfoStatus.AutoOrderWait; break;

                }
                CurrentOrderInfoStatus = status;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        //----批量加急
        //设置可见与不可见
        private void SetVisible()
        {
            if (lblPriority.Visible == true)
            {
                lblPriority.Visible = false;
                cbPriority.Visible = false;
                btnPriorityFind.Visible = false;
                btnPriority.Visible = false;
            }
            else
            {
                lblPriority.Visible = true;
                cbPriority.Visible = true;
                btnPriorityFind.Visible = true;
                btnPriority.Visible = true;
                btnPriority.Enabled = false;
            }
        }

        private void gvOrderInfo_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == "OutShopStartProductTime")
            {
                DateTime time =

                        Convert.ToDateTime(
                            ((OrderInfoBLL.OrderProductUser)(gvOrderInfo.GetRow(e.RowHandle))).OutShopStartProductTime);

                if (time.CompareTo(Convert.ToDateTime("2000-01-01")) < 0)
                {
                    e.DisplayText = "";
                }

            }
            if (e.Column.FieldName == "TechTime")
            {
                DateTime time =

                        Convert.ToDateTime(
                            ((OrderInfoBLL.OrderProductUser)(gvOrderInfo.GetRow(e.RowHandle))).TechTime);

                if (time.CompareTo(Convert.ToDateTime("2000-01-01")) < 0)
                {
                    e.DisplayText = "";
                }

            }
        }

        //-----------------------------列表新增功能-------------------------------------
        //开单
        private void tsStartOrder_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                if (Manage.StartOrderNum > 0)
                {
                    Cursor = Cursors.Default;
                    return;
                }
                Manage.StartOrderNum++;
                StartOrder.FrmStartOrder _startOrder = new MINERP.CustomerManage.StartOrder.FrmStartOrder();
                _startOrder.UserID = Manage.userId;
                _startOrder.UserName = Manage.userName;
                _startOrder.Manage = Manage;

                Cursor = Cursors.Default;

                //绑定刷新处理功能(关闭开单页面后默认不刷新)
                //_startOrder.FormClosed += new FormClosedEventHandler(_startOrder_FormClosed);
                _startOrder.Show();
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }
        }
        //编辑
        private void tsEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvOrderInfo.SelectedRowsCount == 1 && gvOrderInfo.GetSelectedRows()[0] >= 0)
                {
                    Cursor = Cursors.WaitCursor;


                    if (Manage.EditOrderNum > 0)
                    {
                        Cursor = Cursors.Default;
                        return;
                    }
                    Manage.EditOrderNum++;
                    //更新订单为正在编辑状态

                    int orderInfoId = Convert.ToInt32(gvOrderInfo.GetRowCellValue(gvOrderInfo.GetSelectedRows()[0], "OrderProductId"));
                    int orderBagId = bll.GetOrderBagIdByOrderProductId(orderInfoId);

                    StartOrder.FrmEditOrder _editOrder = new StartOrder.FrmEditOrder(orderBagId, orderInfoId);
                    _editOrder.Manage = Manage;
                    _editOrder.UserType = 1;            //表示是普通的编辑调用
                    _editOrder.EditUserId = Manage.userId;
                    //添加更新处理程序
                    //_editOrder.FormClosed += new FormClosedEventHandler(_editOrder_FormClosed);
                    _editOrder.ShowDialog();
                }
                else
                {
                    MessageBox.Show("请先选择订单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    return;
                }
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        //单击路径跳转到真实目录
        private void gvOrderInfo_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            try
            {
                if (e.RowHandle >= 0 && e.Column.FieldName == "OrderProductPath")
                {
                    GridView view = sender as GridView;
                    string infoPath = view.GetRowCellValue(e.RowHandle, "OrderProductPath").ToString();
                    ShellExecute(IntPtr.Zero, new StringBuilder("open"), new StringBuilder("explorer.exe"),
                    new StringBuilder(infoPath), new StringBuilder(string.Empty), 1);
                }
                else if (e.Clicks == 2)
                {
                    //双击其他字段展开产品明细
                    if (gcOrderInfo.DataSource == null || gvOrderInfo.SelectedRowsCount != 1)
                    {
                        return;
                    }
                    if (gvOrderInfo.GetSelectedRows()[0] >= 0)                                              //组列不作处理
                    {
                        Cursor = Cursors.WaitCursor;
                        OrderInfoBLL.OrderProductUser currentOrderInfo = gvOrderInfo.GetRow(gvOrderInfo.GetSelectedRows()[0]) as OrderInfoBLL.OrderProductUser;
                        FrmOrderInfoDetail frm = FrmOrderInfoDetail.GetInstance;
                        frm.CurrentOrderInfo = currentOrderInfo;
                        frm.Show();
                        Cursor = Cursors.Default;
                    }
                }
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                throw ex;
            }
        }

        //停止产品
        private void tsStop_Click(object sender, EventArgs e)
        {

        }

        //恢复产品
        private void tsRecover_Click(object sender, EventArgs e)
        {

        }

        //通信
        private void tsCommunicate_Click(object sender, EventArgs e)
        {

        }

        //显示产品明细按钮
        private void tsDetail_Click(object sender, EventArgs e)
        {
            try
            {
                if (gcOrderInfo.DataSource == null || gvOrderInfo.SelectedRowsCount != 1)
                {
                    return;
                }
                if (gvOrderInfo.GetSelectedRows()[0] >= 0)                                              //组列不作处理
                {
                    Cursor = Cursors.WaitCursor;
                    OrderInfoBLL.OrderProductUser currentOrderInfo = gvOrderInfo.GetRow(gvOrderInfo.GetSelectedRows()[0]) as OrderInfoBLL.OrderProductUser;
                    FrmOrderInfoDetail frm = FrmOrderInfoDetail.GetInstance;
                    frm.CurrentOrderInfo = currentOrderInfo;
                    frm.Show();
                    Cursor = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //财务审核那些欠款的产品
            if (MessageBox.Show("确定要使欠款的产品恢复生产？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                return;
            try
            {
                int[] indexes = gvOrderInfo.GetSelectedRows();
                if (indexes.Length == 0)
                {
                    MessageBox.Show("没有产品被选中！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                //批量加急
                List<int> orderProductIds = new List<int>();
                foreach (int index in indexes)
                {
                    OrderInfoBLL.OrderProductUser op = gvOrderInfo.GetRow(index) as OrderInfoBLL.OrderProductUser;
                    if (op.Status == (int)COM.OrderInfoStatus.WaitPay || op.Status == (int)COM.OrderInfoStatus.WaitCheck || op.Status == (int)COM.OrderInfoStatus.WaitPayAndWaitMovePhoto)
                    {
                        orderProductIds.Add(op.OrderProductId);
                    }                    
                }
                if (orderProductIds.Count > 0)
                {
                    bll.SetOrderProduct(orderProductIds);
                    MessageBox.Show("审核成功！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            //将移动失败的图片再次移动图片
            try
            {
                int[] indexes = gvOrderInfo.GetSelectedRows();
                if (indexes.Length == 0)
                {
                    MessageBox.Show("没有产品被选中！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                
                List<int> orderProductIds = new List<int>();
                foreach (int index in indexes)
                {
                    OrderInfoBLL.OrderProductUser op = gvOrderInfo.GetRow(index) as OrderInfoBLL.OrderProductUser;
                    if (op.Status == (int)COM.OrderInfoStatus.WaitPhotoMove || op.Status == (int)COM.OrderInfoStatus.WaitPayAndWaitMovePhoto)
                    {
                        orderProductIds.Add(op.OrderProductId);
                    }
                }
                if (orderProductIds.Count > 0)
                {
                    //bll.SetOrderProduct(orderProductIds);
                    bll.TryMovePhotoAgagin(orderProductIds);
                    MessageBox.Show("操作成功！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void toolStripButtonExport_Click(object sender, EventArgs e)
        {
            gcOrderInfo.ExportToExcel(this, "产品列表信息");
            //SaveFileDialog saveFileDialog = new SaveFileDialog();
            //saveFileDialog.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
            ////saveFileDialog.AddExtension = true;
            //saveFileDialog.FilterIndex = 3;
            //saveFileDialog.RestoreDirectory = true;
            //if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    string fileName = saveFileDialog.FileName;
            //    if (saveFileDialog.OpenFile() != null)
            //    {
            //        switch (saveFileDialog.FilterIndex)
            //        {
            //            case 3:
            //                gvOrderInfo.ExportToXls(fileName);
            //                return;
            //            case 4:
            //                gvOrderInfo.ExportToHtml(fileName);
            //                return;
            //            case 2:
            //                gvOrderInfo.ExportToPdf(fileName);
            //                return;
            //            case 1:
            //                gvOrderInfo.ExportToText(fileName);
            //                return;
            //            default:
            //                break;
            //        }
            //    }

            //}
        }

        private void toolStripButtonAutoOederCheck_Click(object sender, EventArgs e)
        {
            //财务审核那些欠款的产品
            if (MessageBox.Show("确定要生成远程开单的产品吗？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                return;
            try
            {
                int[] indexes = gvOrderInfo.GetSelectedRows();
                if (indexes.Length == 0)
                {
                    MessageBox.Show("没有产品被选中！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                
                List<int> orderProductIds = new List<int>();
                foreach (int index in indexes)
                {
                    OrderInfoBLL.OrderProductUser op = gvOrderInfo.GetRow(index) as OrderInfoBLL.OrderProductUser;
                    if (op.Status == (int)COM.OrderInfoStatus.AutoOrderWait)
                    {
                        orderProductIds.Add(op.OrderProductId);
                    }
                }
                if (orderProductIds.Count > 0)
                {
                    bll.SetOrderProduct(orderProductIds);
                    MessageBox.Show("审核成功！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

    }
}
